/*
 * ModulePackageManager.java
 *
 * Created on June 18, 2007, 8:46 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package tal.drivechain.plugin;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import tal.drivechain.system.Config;
import tal.drivechain.module.xmlconfig.PackageConfiguration;
import tal.drivechain.system.ConfigFile;

/**
 * An abstract class that's responsible for registering a new modules package
 * with the applications {@link ConfigFile} and for any other steps required for installing a new
 * package, such as copying the package to a new location or any other requirements.
 * <p>
 * When the user wants to install this package the application will call the install method which
 * in turn should return a Config object that will be written to the config file.
 * </p>
 * <p>
 * The class is also used for getting a reference to a packages package.xml. The default constructor
 * assumes the file is in /manager/package.xml path inside the package.
 * ModulePackageManager classes must be inside a java package named manager in the module package file
 * JAR file.
 * </p>
 * @author Tal Eisenberg
 */
public abstract class ModulePackageManager {
    private File path = null;
    private PackageConfiguration packageConf;

    public ModulePackageManager() {
        try {
            URI uri = this.getClass().getResource("/manager/package.xml").toURI();
            packageConf = new PackageConfiguration(uri);
        } catch (URISyntaxException e) { System.out.println("Error while loading package configuration: " + e.getMessage()); }
    }

    /**
     * Returns the a PackageConfiguration object that contains help and structure 
     * information from the package xml file.
     */
    public PackageConfiguration getPackageConfiguration() { return packageConf; }

    public File getPath() {
        return path;
    }
    
    
    /**
     * Returns a an empty Config object as default. Override this method if your module package
     * needs to write anything to the config file.
     *
     * @param installProperties   Any properties that were supplied with the --install command line argument,
     *                      in the format of --install packagePath:installProperties
     */
    public Config install(String installProperties) {
        Config conf = new Config(this);
        return conf;
    }

    public void setPath(File path) {
        this.path = path;
    }
}
